/*
 * 路由跳转管理
 */

export function navigatetopage(url) {
	const tablist = ["pages/index/index", "pages/group/index", "pages/pyq/index", "pages/message/index",
		"pages/mine/mine"
	]
	if (tablist.indexOf(url) > -1) {
		uni.switchTab({
			url: url
		})
	} else {
		uni.navigateTo({
			url: url
		})
	}
}
/*
 * 转换时间为 xx前
 */
export function timetobefor(dateTimeStamp) {

	// 时间字符串转时间戳 获取毫秒数
	var timestamp = dateTimeStamp;
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var year = day * 365;
	var now = new Date().getTime();
	var diffValue = now - timestamp;
	var result;
	if (diffValue < 0) {
		return;
	}
	var yearC = diffValue / year;
	var monthC = diffValue / month;
	var weekC = diffValue / (7 * day);
	var dayC = diffValue / day;
	var hourC = diffValue / hour;
	var minC = diffValue / minute;
	if (yearC >= 1) {
		result = "" + parseInt(yearC) + "年前";
	} else if (monthC >= 1) {
		result = "" + parseInt(monthC) + "月前";
	} else if (weekC >= 1) {
		result = "" + parseInt(weekC) + "周前";
	} else if (dayC >= 1) {
		result = "" + parseInt(dayC) + "天前";
	} else if (hourC >= 1) {
		result = "" + parseInt(hourC) + "小时前";
	} else if (minC >= 1) {
		result = "" + parseInt(minC) + "分钟前";
	} else
		result = "刚刚";
	return result;

}
export function timespantoYMD(originVal) {
	// 先把传参毫秒转化为new Date()
	const dt = new Date(originVal)
	const y = dt.getFullYear()
	// 月份是从0开始,需要+1
	// +''是把数字转化为字符串,padStart(2,'0')是把字符串设置为2位数,不足2位则在开头加'0'
	const m = (dt.getMonth() + 1 + '').padStart(2, '0')
	const d = (dt.getDate() + '').padStart(2, '0')

	return `${y}-${m}-${d}`
}
export function timespantoYMDhms(originVal) {
	// 先把传参毫秒转化为new Date()
	const dt = new Date(originVal)
	const y = dt.getFullYear()
	// 月份是从0开始,需要+1
	// +''是把数字转化为字符串,padStart(2,'0')是把字符串设置为2位数,不足2位则在开头加'0'
	const m = (dt.getMonth() + 1 + '').padStart(2, '0')
	const d = (dt.getDate() + '').padStart(2, '0')
	const h = dt.getHours()
	const mm = dt.getMinutes()
	const s = dt.getSeconds()
	return `${y}-${m}-${d} ${h}:${mm}:${s}`
}
export function encryptphone(phone) {
	return `${phone.substr(0, 3)}****${phone.substr(7)}`
}

// 数组置顶
export function toFirst(arr, index) {
	if (index != 0) {
		arr.unshift(arr.splice(index, 1)[0]);
	}
	return arr;
}

function imageCompress(file) {
	return new Promise((resolve, reject) => {
		let { size, path } = file
		let type = path.split(".")[1]
		console.log(51, size, path, type)
		//大于100kb进行压缩
		if (size <= 100 * 1024) {
			resolve(file)
			console.log(56, file)
			return false
		}
		uni.compressImage({
			src: path,
			quality: 80,
			width: '50%',
			height: '50%',
			success: res => {
				console.log(62, res)
				let newPath = res.tempFilePath + type
				console.log(622, newPath)
				uni.getFileInfo({
					filePath: res.tempFilePath,
					success: async (info) => {
						console.log(73, info)
						let newFile = { ...file, size: info.size, path: newPath,
							tempFilePath: res.tempFilePath }
						resolve(await imageCompress(newFile))
					}
				})
			},
			fail: err => {
				console.log(63, err)
			}
		})
	})
}